package com.dante.mvp_login.view;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dante.mvp_login.R;
import com.dante.mvp_login.contract.UserContract;
import com.dante.mvp_login.presenter.LoginPresenterImpl;
import com.rengwuxian.materialedittext.MaterialEditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, UserContract.LoginView {

    /*===== 控件相关 =====*/
    private MaterialEditText nameEText;
    private MaterialEditText pwdEText;
    private Button registerButton;
    private Button loginButton;

    /*===== 数据相关 =====*/
    private Toast mToast;
    private String inputName;
    private String inputPwd;
    private LoginPresenterImpl mLoginPresenter;
    private String mName;
    private String mPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    /**
     * 初始化View
     */
    private void initView() {
        nameEText = (MaterialEditText) findViewById(R.id.met_main_name);
        pwdEText = (MaterialEditText) findViewById(R.id.met_main_pwd);
        registerButton = (Button) findViewById(R.id.bt_main_register);
        loginButton = (Button) findViewById(R.id.bt_main_login);

        registerButton.setOnClickListener(this);
        loginButton.setOnClickListener(this);
    }

    /**
     * 初始化Data
     */
    private void initData() {
        //需要持有一个 Presenter 的实例
        //测试
        mLoginPresenter = new LoginPresenterImpl(this);
    }

    @Override
    public void onClick(View view) {
        mName = nameEText.getText().toString().trim();
        mPwd = pwdEText.getText().toString().trim();
        switch (view.getId()) {
            case R.id.bt_main_register:
                if (mLoginPresenter != null){
                    mLoginPresenter.registerUser(mName, mPwd);
                }
                break;
            case R.id.bt_main_login:
                if (mLoginPresenter != null){
                    mLoginPresenter.loginUser(mName, mPwd);
                }
                break;
            default:
                break;
        }

    }

    @Override
    public void onCheckText(boolean isNull) {

    }

    @Override
    public void onRegisterSucceed() {
          Toast.makeText(this,"注册成功",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRegisterfailed() {
        Toast.makeText(this,"注册失败",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onLoginSucceed() {
        Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onLoginfailed() {
        Toast.makeText(this,"登录失败",Toast.LENGTH_LONG).show();
    }
}
